#include "ManageRoute.h"
#include "Beacon.h"
#include "Recv.h"
#include "Constant.h"

#include<pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#include <signal.h>

struct sigaction sa;

void sigpipe(){
    printf("sigpipe\n");
}

//linkManageInit()


int main(){
    sa.sa_handler = sigpipe;
    sigaction(SIGPIPE,&sa, 0);
    constant_init();
    printf("HOSTIP=%s,BROADCASTIP=%s,\n",HOSTIP,BROADCASTIP);
    beacon_init();
    ManageRoute_init();
    recv_init();


    pthread_t id;
    pthread_attr_t attr;
    pthread_attr_init (&attr);
    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);


    if( pthread_create(&id,&attr,(void*)recv_departure,NULL) !=0 ){
        //perror("beacon start error");
        printf("recv start error!%s\n",strerror(errno));
        //beep(2000,2000);
        abort();
        exit(1);
    }

    if( pthread_create(&id,&attr,(void*)beacon,NULL) !=0 ){
        //perror("beacon start error");
        printf("beacon start error!%s\n",strerror(errno));
        //beep(2000,2000);
        abort();
        exit(1);
    }

	if( pthread_create(&id,&attr,(void*)ManageRoute,NULL) !=0 ){
	    //perror("broadcastRoute start error");
	    printf("ManageRoute start error!%s\n",strerror(errno));
	    //beep(2000,2000);
	    abort();
	    exit(1);
    }
    pthread_attr_destroy(&attr);

    printf("--------------------Static Router has started successfully-------------------\n");
    while(1){
        sleep(1000);
    }
}
